$(function () {
    // 获取联系人信息
    let ticket = $.fn.ticket,
        passengers = ticket.storage.get('passengers');

    //渲染联系人
    $.showLoading();
    if (!passengers) {
        ticket.doAjax({
            url: ticket.url.passengers.get,
            success: function (resp) {
                $.hideLoading();
                switch (Number(resp.error)) {
                    case 0:
                        passengers = resp.data.data.normal_passengers;
                        ticket.storage.set('passengers', passengers);
                        render_passengers(passengers);
                        break;
                    case 2:
                    case 3:
                        $.alert('该用户已在其他地点登录，点击确认后重新登录', function () {
                            ticket.login({refresh: true});
                        });
                        break;
                    default:
                        console.log({error: resp});
                        ticket.cacheTrain(); // 重新缓存车次信息
                        $.alert('获取联系人信息失败', function () {
                            history.back();
                        });
                        break;
                }
            },
            error: function (xhr) {
                $.hideLoading();
                console.log(xhr);
                $.alert(xhr.statusText, '获取联系人信息失败', function () {
                    history.back();
                });
            }
        });
    } else {
        $.hideLoading();
        render_passengers(passengers);
    }

    // 渲染联系人信息
    function render_passengers(data) {
        let userList = ticket.storage.get('userList');
        $.each(data, function (idx) {
            let status, pass = false, id = Math.random(), that = this,
                text = $('#clone_userInfo').clone().text().replace('用户姓名', this.passenger_name).replace(
                    '证件类型', this.passenger_id_type_name).replace(/单选按钮/g, id).replace(/-1/g, idx).replace('证件号码',
                    function () {
                        let code = that.passenger_id_no;
                        switch (that.passenger_id_type_name) {
                            case '二代身份证':
                                code = code.substr(0, 6) + '********' + code.substr(14, 4);
                                break;
                            case '港澳通行证':
                                code = code.substr(0, 5) + '*****';
                                break;
                            case '台湾通行证':
                                code = code.substr(0, 3) + '****' + code.substr(7, 2);
                                break;
                            case '护照':
                                code = code.substr(0, 3) + '****' + code.substr(7, 2);
                                break;
                        }
                        return code;
                    });
            if (this.passenger_id_type_code === '2' || ['94', '96'].indexOf(this.total_times) !== -1) {
                status = '未通过';
            } else {
                if (['95', '97'].indexOf(this.total_times) !== -1) {
                    status = '已通过';
                    pass = true;
                } else if (['93', '99'].indexOf(this.total_times) !== -1) {
                    pass = true;
                    (this.passenger_id_type_code === '1') ? status = '已通过' : status = '预通过';
                } else if (['92', '98'].indexOf(this.total_times) !== -1) {
                    if (this.passenger_id_type_code === "B" ||
                        this.passenger_id_type_code === "H" ||
                        this.passenger_id_type_code === "C" ||
                        this.passenger_id_type_code === "G") {
                        status = '请报验';
                    } else {
                        status = '待核验';
                    }
                } else if (this.total_times === '91') {
                    if (this.passenger_id_type_code === "B" ||
                        this.passenger_id_type_code === "H" ||
                        this.passenger_id_type_code === "C" ||
                        this.passenger_id_type_code === "G") {
                        status = '请报验';
                    }
                } else {
                    status = '请报验';
                }
            }
            $('dl').append(text.replace('状态', status).replace('status-normal', pass ? 'status-access' : 'status-deny'));

            if (userList) {
                $.each(userList, function () {
                    if (this.passenger_name === that.passenger_name) {
                        $('label[for="' + id + '"]').click();
                    }
                });
            }
        });
    }

    // 最多选择5个人
    $('input[type="radio"]').click(function () {
        let $input = $(':checked'),
            max_num = 5,
            $that = $(this);
        if ($input.length > max_num) {
            return $.alert(`最多选择${max_num}人`, function () {
                $that.click();
            });
        }
    });

    // 页面跳转
    $('#submit').click(function () {
        let url, userList = $.map($(':checked'), function (input) {
            return passengers[$(input).data('idx')];
        });
        if (userList.length === 0) {
            return $.alert('请选择乘客');
        }
        if (ticket.storage.get('isGeabTicket')) {
            url = 'grabTicket.html';
        } else {
            url = 'trainBook.html';
        }
        ticket.storage.set('userList', userList);
        window.location.href = url;
    });

    // 修改用户信息
//        $(document).on('click', '.icon-bianji', function () {
//            ticket.set('info', passengers[$(this).data('idx')])
//            window.location.href = 'trainPerson_Detail.html';
//        });

    // 核验信息
    $('#verification').click(function () {
        $('.alert').show();
    });

    $('.train_mask').click(function () {
        $('.alert').hide();
    });
});